<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属工厂" prop="factoryId">
        <el-select
          v-model="queryParams.factoryId"
          placeholder="请选择所属工厂"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="dict in deptOptions"
            :key="dict.deptId"
            :label="dict.name"
            :value="dict.deptId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['equipment:depot:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="depotList"
      row-key="id"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column label="名称" prop="name" />
      <el-table-column label="编号" prop="code" align="center" />
      <el-table-column
        label="类型"
        align="center"
        prop="type"
        :formatter="typeFormat"
      />
      <!--      <el-table-column label="父级" align="center" prop="parentId" />-->
      <el-table-column label="所属工厂" align="center" prop="factoryName" />
      <!--      <el-table-column label="负责人" align="center" prop="dutyPeopleName" />-->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['equipment:depot:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['equipment:depot:add']"
            >新增</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['equipment:depot:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改库位对话框 -->
    <el-dialog
      v-myFullscreen
      :title="title"
      :visible.sync="open"
      width="700px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="编号" prop="code">
              <el-input
                v-model="form.code"
                placeholder="请输入编号"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入名称"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型" prop="type">
              <el-select
                v-model="form.type"
                placeholder="请选择类型"
                :disabled="openDisabled"
              >
                <el-option
                  v-for="dict in typeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="父级" prop="parentId">
              <treeselect
                :searchable="false"
                v-model="form.parentId"
                :options="depotOptions"
                :normalizer="normalizer"
                placeholder="请选择父级"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属工厂" prop="factoryId">
              <el-select
                v-model="form.factoryId"
                placeholder="请选择所属工厂"
                :disabled="openDisabled"
              >
                <el-option
                  v-for="dict in deptOptions"
                  :key="dict.deptId"
                  :label="dict.name"
                  :value="dict.deptId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人" prop="dutyPeople">
              <UserDialog
                @dialogChange="form.dutyPeople = $event.value"
                :echoValue="form.dutyPersonName"
                value="userName"
                :multiple="false"
                :openDisabled="openDisabled"
                placeholder="请选择责任人"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话" prop="phone">
              <el-input
                v-model="form.phone"
                placeholder="请输入电话"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址" prop="address">
              <el-input
                v-model="form.address"
                placeholder="请输入地址"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经度" prop="lng">
              <el-input
                v-model="form.lng"
                placeholder="请输入经度"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="维度" prop="lat">
              <el-input
                v-model="form.lat"
                placeholder="请输入维度"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDepot,
  getDepot,
  delDepot,
  addDepot,
  updateDepot,
  exportDepot,
} from "@/api/equipment/materials/depot";
import { getSelectList } from "@/api/basic/workshop/workshop";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Depot",
  components: {
    Treeselect,
  },
  data() {
    return {
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 库位表格数据
      depotList: [],
      // 库位树选项
      depotOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型字典
      typeOptions: [],
      // 查询参数
      queryParams: {
        code: null,
        name: null,
        type: null,
        materialId: null,
        parentId: null,
        ancestors: null,
        factoryId: null,
        dutyPeople: null,
      },
      // 部门树选项
      deptOptions: undefined,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [{ required: true, message: "不能为空", trigger: "blur" }],
        name: [{ required: true, message: "不能为空", trigger: "blur" }],
        type: [{ required: true, message: "不能为空", trigger: "blur" }],
        factoryId: [{ required: true, message: "不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("depot_space_type").then((response) => {
      this.typeOptions = response.data;
    });
    this.getDeptTreeselect();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getDeptTreeselect() {
      getSelectList().then((response) => {
        this.deptOptions = response.data;
      });
    },
    /** 查询库位列表 */
    getList() {
      this.loading = true;
      listDepot(this.queryParams).then((response) => {
        this.depotList = this.handleTree(response.data, "id", "parentId");
        this.loading = false;
      });
    },
    /** 转换库位数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children,
      };
    },
    /** 查询库位下拉树结构 */
    getTreeselect() {
      listDepot().then((response) => {
        this.depotOptions = [];
        const data = { id: 0, name: "顶级节点", children: [] };
        data.children = this.handleTree(response.data, "id", "parentId");
        this.depotOptions.push(data);
      });
    },
    // 类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        type: null,
        materialId: null,
        parentId: null,
        ancestors: null,
        factoryId: null,
        dutyPeople: null,
        phone: null,
        address: null,
        lng: null,
        lat: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        remark: null,
        dutyPeopleName: null,
        materialName: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.openDisabled = false;
      this.getTreeselect();
      if (row != null && row.id) {
        this.form.parentId = row.id;
      } else {
        this.form.parentId = 0;
      }
      this.form.factoryId = row.factoryId;
      this.form.dutyPeople = row.dutyPeople;
      this.form.dutyPeopleName = row.dutyPeopleName;
      this.form.phone = row.phone;
      this.form.lng = row.lng;
      this.form.lat = row.lat;
      this.form.address = row.address;
      this.form.materialId = row.materialId;
      this.form.materialName = row.materialName;
      if (row.type == "01") {
        this.form.type = "02";
      } else if (row.type == "02") {
        this.form.type = "03";
      } else if (row.type == "03") {
        this.form.type = "04";
      } else if (row.type == "11") {
        this.form.type = row.type;
      }
      this.open = true;
      this.title = "添加库位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.id;
      }
      getDepot(row.id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库位";
      });
    },
    /** 修改按钮操作 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.id;
      }
      getDepot(row.id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "查看库位";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateDepot(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDepot(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除库位编号为"' + row.id + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delDepot(row.id);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
